﻿<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="../Include/conn.asp" -->
<!--#include file="seeion.asp" -->
<%call chkAdmin("|20")
SiteUp="upload/image/"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>网站后台管理--文件清理</title>
<link href="images/style.css" type=text/css rel=stylesheet>
<style type="text/css">
body,td,th,p{font-family: "Microsoft YaHei", Arial, "宋体";}
p{line-height:14px; float:12px;}
p span{color:#F00}
</style>
</head>
<body>
<%if request("action")="del" then%>
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
    <tr> 
      <td height="24" class="topnav"><div>上传文件清理</div></td>
    </tr><tr>
<td height=30 bgcolor="#FFFFFF" class="td" style="padding-left:10px">
<% call DelOver() %>
</td>
</tr>
</table>
<%else
call Main()
end if
Sub Main%>
<table width="100%" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#CCCCCC">
    <tr> 
      <td height="24" class="topnav"><div>上传文件清理</div></td>
    </tr><tr>
<td height=30 bgcolor="#FFFFFF" class="td" style="padding-left:10px"><b>注意：</b>请谨慎使用本功能，操作前最好先备份下上传文件夹，以免文件丢失，清理是清理掉不在数据库中存在或正在使用上传的文件。<br>
<br>
如果你的文章特别多(数万)，清理起来可能会非常慢，不推荐使用本功能。
<br><br>当前上传文件夹为：<%=SiteUp%> <input type="button" value="开始清理" class="btn" onClick="if(confirm('确定要清除文件夹<%=SiteUp%>中的垃圾文件?\n\n请谨慎操作！'))location.href='?action=del';return false;" />
</td>
</tr>
</table>
<%
End Sub

Sub DelOver
	Dim Sql,Rs,strFiles,ItemIntro
	strFiles="|"&dir&SiteUp&"|"&SiteLogo
	ItemIntro=""
		
	Sql = "Select img From ad"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
	
	Sql = "Select logo From config"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
		
	Sql = "Select Content,ImagePath,img,DownURL From Content"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
		IF Rs(1) <> "" Then
			strFiles=strFiles&"|"&Rs(1)
		End If
		IF Rs(2) <> "" Then
			strFiles=strFiles&"|"&Rs(2)
		End If
		IF Rs(3) <> "" Then
			strFiles=strFiles&"|"&Rs(3)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
	
	Sql = "Select img From flash"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
	Sql = "Select img From indexflash"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
	
	Sql = "Select logo From link"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
	Rs.MoveNext
	Wend
	Rs.Close
	
	Sql = "Select Sortimg,SortContent From zych_Type"
	Set Rs = Conn.Execute(Sql)
	While Not Rs.Eof
		IF Rs(0) <> "" Then
			strFiles=strFiles&"|"&Rs(0)
		End If
		IF Rs(1) <> "" Then
			strFiles=strFiles&"|"&Rs(1)
		End If
	Rs.MoveNext
	Wend
	Rs.Close	

	Dim tempStr, tempi, TempArray,UpFileType,regEx,Matches,Match
		UpFileType = "gif|jpg|jpeg|bmp|png"
		Set regEx=New Regexp
		regEx.Ignorecase=True
		regEx.Global=True
        regEx.Pattern = "<img.+?[^\>]>" '查询内容中所有 <img..>
        Set Matches = regEx.Execute(ItemIntro)
        For Each Match In Matches
            If tempStr <> "" Then
                tempStr = tempStr & "|" & Match.value '累计数组
            Else
                tempStr = Match.value
            End If
        Next
        If tempStr <> "" Then
            TempArray = Split(tempStr, "|") '分割数组
            tempStr = ""
            For tempi = 0 To UBound(TempArray)
                regEx.Pattern = "src\s*=\s*.+?\.(" & UpFileType & ")" '查询src =内的链接
                Set Matches = regEx.Execute(TempArray(tempi))
                For Each Match In Matches
                    If tempStr <> "" Then
                        tempStr = tempStr & "|" & Match.value '累加得到 链接加$Array$ 字符
                    Else
                        tempStr = Match.value
                    End If
                Next
            Next
        End If
        If tempStr <> "" Then
            regEx.Pattern = "src\s*=\s*" '过滤 src =
            tempStr = regEx.Replace(tempStr, "")
        End If
		Set regEx=Nothing
        strFiles = strFiles & tempStr
	    strFiles = LCase(strFiles)
		Dim i,theFolder,fso,theFile,theSubFolder
		Set Fso=CreateObject("Scripting.FileSystemObject")
		 i = 0

    Set theFolder = fso.GetFolder(Server.MapPath(dir&SiteUp))
    For Each theFile In theFolder.Files
        If InStr(strFiles, LCase(theFile.name)) <= 0 Then
            theFile.Delete True
            i = i + 1
        End If
    Next
    For Each theSubFolder In theFolder.SubFolders
        For Each theFile In theSubFolder.Files
            If InStr(strFiles, LCase(theSubFolder.name & "/" & theFile.name)) <= 0 Then
			    Response.Write "<p>正在清理"&i&"：<span>"&dir&SiteUp&theSubFolder.name & "/" & theFile.name&"</span></p>"
                theFile.Delete True
                i = i + 1
            End If
        Next
		
    Next
	if i<>0 then num=i-1 else num=i
	Response.Write "<p>完成清理:共清理"&num&"没有使用的文件！"
	Response.Write "<script>alert('本次共清理"&num&"个垃圾文件！');history.go(-1);</script>" 
End Sub
%>
</body>
</html>